﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using XiDainChangAn.OA.BLL;
using XiDainChangAn.OA.IBLL;
using XiDainChangAn.OA.WebApp.Models;
using XiDianChangAn.OA.Model;

namespace XiDainChangAn.OA.WebApp.Controllers
{
    [MyLoginCheckFilterAttribute(IsCheck = false)]
    public class UserInfoController : Controller
    {
        // GET: UserInfo
        //UserInfoService userInfoService = new UserInfoService();

        //Spring.Net--使用注入的方式

        public IUserInfoService UserInfoService { get; set; }

        public ActionResult Index()
        {
            return View();
        }

        #region 01，获取所有用户信息操作+JsonResult ShowAllUser()
        /// <summary>
        /// 获取所有用户信息操作
        /// </summary>
        /// <returns></returns>
        public JsonResult ShowAllUser()
        {
            int pageInde = Request["page"] == null ? 1 : int.Parse(Request["page"]);
            int pageSize = Request["rows"] == null ? 1 : int.Parse(Request["rows"]);
            int totalCount;
            var delFalg = (short)XiDianChangAn.OA.Model.Enum.DelFlagEnum.Normal;
            var userList = UserInfoService.GetPageEntitys<long>(pageSize, pageInde, out totalCount, true, u => u.DelFlag == delFalg, u => u.Id).ToList();
            var temp = from u in userList
                       select new
                       {
                           Id = u.Id,
                           UName = u.UName,
                           ShowName = u.ShowName,
                       };
            //客户端就是遍历rows  jq easy-ui就是遍历rows
            return Json(new { rows = temp, total = totalCount }, JsonRequestBehavior.AllowGet);
        } 
        #endregion

        #region 02，添加用户操作+JsonResult AddUser(UserInfo userInfo)
        /// <summary>
        /// 添加用户操作
        /// </summary>
        /// <param name="userInfo">用户对象</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult AddUser(UserInfo userInfo)
        {
            if (ModelState.IsValid)
            {
                userInfo.DelFlag = (short)XiDianChangAn.OA.Model.Enum.DelFlagEnum.Normal;
                userInfo.CreateTime = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
                userInfo.ModfiedOn = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
                userInfo.PassWord = "123";
                userInfo.Remark = string.Empty;
                UserInfoService.Add(userInfo);
            }
            return Content("ok");
        }
        #endregion

        #region 03，用户编辑
        /// <summary>
        /// 获取要编辑的用户的信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult EditUser(int id)
        {
            UserInfo model = UserInfoService.GetEntitys(u => u.Id == id).FirstOrDefault();
            return Json(model,JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 编辑用户信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult EditUser(UserInfo model)
        {
            UserInfoService.UpDate(model);
            return Content("ok");
        } 
        #endregion

        #region 04，删除用户操作+ActionResult Delete(string strId)
        /// <summary>
        /// 删除用户操作
        /// </summary>
        /// <param name="strId"></param>
        /// <returns></returns>
        public ActionResult Delete(string strId)
        {
            string[] str = strId.Split(new char[] { ',' });
            foreach (string item in str)
            {

                UserInfoService.Delete(UserInfoService.GetById(int.Parse(item)));
            }
            return Content("ok");
        } 
        #endregion
    }
}